package com.matrix.index.ui.home.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.asLiveData
import com.matrix.basecore.net.BaseData
import com.matrix.index.net.IndexResult
import com.matrix.index.respository.WallpaperResp
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flow

class BingVM : ViewModel() {

    /**
     * 获取数据
     */
    fun bingImage(): LiveData<IndexResult<out BaseData>> {
        return flow {
            val bingImage = WallpaperResp.bingImage()
            if (bingImage != null) {
                var indexResult = IndexResult(IndexResult.SUCCESS, bingImage)
                emit(indexResult)
            } else {
                var indexResult = IndexResult<BaseData>(IndexResult.FAIL, "fail")
                emit(indexResult)
            }
        }.catch { ex ->
            var indexResult = IndexResult<BaseData>(IndexResult.ERROR, ex.toString())
            emit(indexResult)
        }.asLiveData()
    }

}